package com.yy.train.ticket.component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author yy
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        boolean createTime = metaObject.hasSetter("createTime");
        boolean createUser = metaObject.hasSetter("createUser");
        boolean sysState = metaObject.hasSetter("sysState");

        if (createTime) {
            // 或者 // 起始版本 3.3.3(推荐)
            this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
                  }
        /*if (createUser) {
            String fullName=  TenantContext.getFullname();
            // 或者 // 起始版本 3.3.3(推荐)
            this.strictInsertFill(metaObject, "createUser", String.class, fullName);
        }*/
        if (sysState) {
            // 或者 // 起始版本 3.3.0(推荐使用)
            this.strictInsertFill(metaObject, "sysState", Integer.class, 0);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        boolean updateTime = metaObject.hasSetter("updateTime");
        boolean updateUser = metaObject.hasSetter("updateUser");

        if (updateTime) {
            // 起始版本 3.3.3(推荐)
            //this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
            this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());

        }
       /* if (updateUser) {
            String fullName=  TenantContext.getFullname();
            // 或者 // 起始版本 3.3.3(推荐)
            this.strictUpdateFill(metaObject, "updateUser", String.class, fullName);
        }*/



    }


}
